package card_discount

import (
	// "bp/cache"
	sqlc "bp/db/sqlc"
	"bp/internal/service/param"
	"context"
)

// SetCardDiscountItemsParam 设置卡折扣商品配置
type SetCardDiscountItemsParam struct {
	CardDiscountPtr *sqlc.CardDiscount
	ItemConfPtrs    param.ItemConfigurationPtrArray
}

// SetCardDiscountItems 设置卡折扣商品配置
func (s *CardDiscountEntityServiceImp) SetCardDiscountItems(ctx context.Context, q *sqlc.Queries, ptr *SetCardDiscountItemsParam) (err error) {
	cardDiscountID := ptr.CardDiscountPtr.ID
	err = q.DisableCardDiscountItemsByCardDiscountID(ctx, cardDiscountID)
	if err != nil {
		return
	}
	if len(ptr.ItemConfPtrs) > 0 {
		for idx, itemConf := range ptr.ItemConfPtrs {
			err = q.UpsertCardDiscountItem(ctx, sqlc.UpsertCardDiscountItemParams{
				CardDiscountID: cardDiscountID,
				OutCardID:      ptr.CardDiscountPtr.OutCardID,
				ItemID:         itemConf.ID,
				ItemName:       itemConf.Name,
				SortNo:         int32(idx + 1),
			})
			if err != nil {
				return
			}
		}
	}
	return
}
